const {secretKey} = require('../jwt/index');
let blackList = ['/users/site', '/users/list', '/users/detail'];
const jwt = require('jsonwebtoken');
module.exports = function () {
    return async function (ctx, next) {
        const original = ctx.originalUrl.split('?')[0];
        if (blackList.includes(original)) {
            let token = ctx.headers['authorization'];
            let userInfo = ctx.session[token];
            if (!token || !userInfo) {
                ctx.status = 401;
                ctx.body = {code: 401, msg: '请登录'};
            } else {
                const verify = await jwt.verify(token, secretKey);
                if (verify) {
                    await next();
                } else {
                    ctx.status = 401;
                    ctx.body = {code: 401, msg: '请登录'};
                }
            }
        } else {
            await next();
        }
    }
};